﻿using Max.PH5_2006.ShoppingSite.IService;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Max.PH5_2006.ShoppingSite.DemoAPI.Controllers
{
    /// <summary>
    /// 商品控制器
    /// </summary>
    [Route("goods")]
    [ApiController]
    public class GoodsController : ControllerBase
    {
        IGoodsService _servce;
        ICategoryService _categoryServce;

        public GoodsController(IGoodsService servce, ICategoryService categoryServce)
        {
            _servce = servce;
            _categoryServce = categoryServce;
        }

        /// <summary>
        /// 商品显示查询
        /// </summary>
        /// <param name="prices">prices是价格区间，格式是 0-0（最小值-最大值）</param>
        /// <returns></returns>
        [HttpGet("query")]
        public IActionResult Query(string prices = "", string? categoryId = "")
        {
            //1、先拿到所有商品
            var data = _servce.Query();

            if (!string.IsNullOrEmpty(prices))
            {
                var p = prices.Split('-'); //前台传过来的是 100-200这样的价格范围
                int min = Convert.ToInt32(p[0]);   //100   最小值
                int max = Convert.ToInt32(p[1]);   //200   最大值

                //价格区间查询，就是价格：大于最小值，小于最大值
                 data = data.Where(x => x.SalePrice >= min && x.SalePrice <= max).ToList();

            }

            //查询对应的分类
            if (!string.IsNullOrEmpty(categoryId)) { 
                data = data.Where(x => categoryId.Equals(x.CategoryId.ToString())).ToList();
            }

            return Ok(data);
        }

        /// <summary>
        /// 分类
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("category")]
        public IActionResult GetCategory(string? id = "")
        {
            //什么都不值 ，默认是只拿第一级分类

            if (string.IsNullOrEmpty(id))
                id = new Guid().ToString();   //id是guid类型，一级分类的pid全是0，所以用new guid()

            var data = _categoryServce.Query().Where(x => x.PId == new Guid(id));

            return Ok(data);
        }
    }
}
